<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上班打卡时间" prop="checkInTime">
        <el-date-picker clearable
                        v-model="queryParams.checkInTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择上班打卡时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下班打卡时间" prop="checkOutTime">
        <el-date-picker clearable
                        v-model="queryParams.checkOutTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下班打卡时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="submitForm"
          v-hasPermi="['system:attendance:add']"
        >打卡打卡!</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-data-analysis"
          size="mini"
          @click="showAttendanceStats"
          v-hasPermi="['system:attendance:stats']"
        >打卡统计</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attendanceList"  @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="用户ID" align="center" prop="userId" width="220" />
      <el-table-column label="用户名" align="center" prop="username" width="250"/>
      <el-table-column label="上班打卡时间" align="center" prop="checkInTime" width="370">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkInTime) ,'YYYY-MM-DD HH:mm:ss'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="下班打卡时间" align="center" prop="checkOutTime" width="370">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkOutTime) ,'YYYY-MM-DD HH:mm:ss'}}</span>
        </template>
      </el-table-column>
      <!-- 调用字典来显示状态 -->
      <el-table-column prop="status" label="状态" width="200" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_attendance_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handlePagination"
    />


    <!-- 打卡统计弹窗 -->
    <el-dialog :title="statsTitle" :visible.sync="statsVisible" width="600px" append-to-body>
      <el-form ref="statsForm" :model="statsForm" label-width="120px">
        <el-form-item label="出勤总数">
          <el-input v-model="statsForm.totalAttendance" readonly />
        </el-form-item>
        <el-form-item label="早退数">
          <el-input v-model="statsForm.earlyLeaveCount" readonly />
        </el-form-item>
        <el-form-item label="迟到数">
          <el-input v-model="statsForm.lateCount" readonly />
        </el-form-item>
        <el-form-item label="迟到早退数">
          <el-input v-model="statsForm.lateAndEarlyLeaveCount" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="statsVisible = false">取 消</el-button>
        <el-button type="primary" @click="statsVisible = false">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listAttendance,
  getAttendance,
  delAttendance,
  addAttendance,
  updateAttendance,
  getAttendanceStats
} from "@/api/system/attendance"

export default {

  name: "Attendance",
  dicts: ['sys_attendance_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤记录表格数据
      attendanceList: [],
      // 历史打卡记录数据
      historyAttendanceList: [],
      // 历史打卡记录加载状态
      historyLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        username: null,
        checkInTime: null,
        checkOutTime: null,
        status: null,
      },
      //用户Id
      currentUserId: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
      },
      // 统计弹窗相关
      statsVisible: false,
      statsTitle: '打卡统计',
      statsForm: {
        totalAttendance: 0,
        earlyLeaveCount: 0,
        lateCount: 0,
        lateAndEarlyLeaveCount: 0
      }
    }
  },
  created() {
    this.getList()

  },
  methods: {
    /** 查询考勤记录列表 */
    getList() {
      this.loading = true
      listAttendance(this.queryParams).then(response => {
        this.attendanceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        username: null,
        checkInTime: null,
        checkOutTime: null,
        status: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAttendance(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改考勤记录"
      })
    },
    /** 打卡按钮 */
    submitForm() {
      addAttendance().then(response => {
        this.$modal.msgSuccess("新增成功")
        this.open = false
        this.getList()
      })
    },
    handlePagination(val) {
      // val 是分页参数，包含 pageNum 和 pageSize
      this.queryParams.pageNum = val.pageNum
      this.queryParams.pageSize = val.pageSize
      this.getList() // 调用查询方法获取新数据
    },
    /** 显示打卡统计 */
    showAttendanceStats() {
      this.statsVisible = true
      this.getAttendanceStats()
    },
    /** 获取考勤统计数据 */
    getAttendanceStats() {
      getAttendanceStats().then(response => {
        console.log(response)
         this.statsForm.totalAttendance = response.totalAttendance
         this.statsForm.earlyLeaveCount = response.earlyLeaveCount
         this.statsForm.lateCount = response.lateCount
         this.statsForm.lateAndEarlyLeaveCount = response.lateAndEarlyLeaveCount
       })
    },
  }
}
</script>
